Titanium.include('../general/GeneralVariables.js');
Titanium.include('../general/GeneralFunctions.js');
Titanium.include('../helpers/windowStackHelper.js');
Titanium.include('../tableViews/HorizontalTableView.js');
Titanium.include('../imageViews/imageView.js');
Titanium.include('../buttons/NavigationButtons.js');
Titanium.include('../menu/menuView.js');

// variable declarations
var currentWindow;
var menuBottomView;
var menuTopView;
Ti.App.stripIndex = -1;

// function declaration

function _onTableSingleTapHandler(event)
{
	if (menuBottomView.showing)
	{
		_hideMenu(menuTopView, menuBottomView);
	}
	else
	{
		_showMenu(menuTopView, menuBottomView);
	}
}

function _onLeftNavigationButtonClickHandler(event)
{
	if (Ti.App.stripIndex < stripViewStack.length - 1)
	{
		_hideMenu(menuTopView, menuBottomView);
		
		var currentStrip = stripViewStack[Ti.App.stripIndex];
		var nextStrip = stripViewStack[Ti.App.stripIndex + 1];
		
		var speed = nextStrip.realWidth / 480;
		nextStrip.visible = true;
		
		if (!isAndroid)
		{
			currentStrip.animate({
				left: 480,
				duration: 400
			}, function ()
			{
				currentStrip.visible = false;
			});
			
			nextStrip.animate({
				left: 0,
				duration: 200 * speed
			});
		}
		else
		{
			currentStrip.animate({
				opacity: 0,
				duration: 800
			}, function ()
			{
				currentStrip.visible = false;
			});
			
			nextStrip.left = 0;
			nextStrip.animate({
				opacity: 1,
				duration: 800
			});
		}
	
		// rebuild menu and put on top
		if (!isAndroid)
		{
			menuBottomView.zIndex = 9999;
			menuTopView.zIndex = 9998;
		}
		
		Ti.App.stripIndex++;
	}
}

function _onRightNavigationButtonClickHandler(event)
{
	if (Ti.App.stripIndex != 0)
	{
		_hideMenu(menuTopView, menuBottomView);
		
		var currentStrip = stripViewStack[Ti.App.stripIndex];
		var nextStrip = stripViewStack[Ti.App.stripIndex - 1];
		
		nextStrip.visible = true;
		
		var speed = nextStrip.realWidth / 480;
		
		if (!isAndroid)
		{
			nextStrip.left = 479;
			nextStrip.animate({
				left: 0,
				duration: 800
			});
			
			currentStrip.left = (-currentStrip.realWidth) + 480;
			
			currentStrip.animate({
				left: (-currentStrip.realWidth) - 480,
				duration: 800
			}, function()
			{
				currentStrip.visible = false;
			});
		}
		else
		{
			nextStrip.left = 0;
			nextStrip.animate({
				opacity: 1,
				duration: 800
			});
			
			currentStrip.animate({
				opacity: 0,
				duration: 800
			}, function()
			{
				currentStrip.visible = false;
			});
		}
		
		Ti.App.stripIndex--;
		
		Ti.App.fireEvent('setCommentData');
	}
}

function _createStripView()
{
	var contentWidth;
	var contentHeight;
	
	if (isAndroid)
	{
		contentWidth = 'auto';
		contentHeight = 320;
	}
	else
	{
		contentWidth = 'auto';
		contentHeight = 'auto';
	}
	
	var scrollView = Titanium.UI.createScrollView({
		contentWidth: contentWidth, 
		contentHeight: contentHeight,
		height: 320,
		width: 480,
		showHorizontalScrollIndicator: true
	});
	
	trace('Ti.App.stripIndex: ' + Ti.App.stripIndex);
	
	var viewArray = [];
	
	/*** TEMPORARY!! (totaal - 1) ***/
	if (Ti.App.stripIndex != 9)
	{
		var leftNavigationButton = _createLeftNavigationButton();
		leftNavigationButton.addEventListener('click', _onLeftNavigationButtonClickHandler);
		viewArray.push(leftNavigationButton);
	}
	
	// alleen bij de eerste vast laden, van de rest alleen tijdelijke dingen maken! (img = eerst local, daarna opnieuw laden)
	for (var i =0; i < 4; i++)
	{
		var view = _createImageView('../images/img01.jpg');
		viewArray.push(view);
	}	
	
	if (Ti.App.stripIndex != 0)
	{
		var rightNavigationButton = _createRightNavigationButton();
		rightNavigationButton.addEventListener('click', _onRightNavigationButtonClickHandler);
		viewArray.push(rightNavigationButton);
	}
	
	var horizontalTable = _createHorizontalTableView(viewArray);
	scrollView.add(horizontalTable);
	
	horizontalTable.addEventListener('singletap', _onTableSingleTapHandler);
	
	scrollView.realWidth = horizontalTable.width;
	
	return scrollView;
}

function _windowSettings()
{
	currentWindow = Titanium.UI.currentWindow;
}

function _createView()
{
	/*** EVENTS ZIJN TIJDELIJK! ***/
	menuBottomView = _createMenuBottomView();
	menuBottomView.addEventListener('click', function(){
		Ti.App.fireEvent('openComments');
	});
	
	menuTopView = _createMenuTopView();
	menuTopView.addEventListener('click', function(){
		Ti.App.fireEvent('openTwitterWindow');
	});
	
	var views = [];
	
	for (var i = 0; i < 10; i++)
	{
		Ti.App.stripIndex++;
		
		var scrollView = _createStripView();
		views.push(scrollView);
		currentWindow.add(scrollView);
	}
	
	_setViewStack(views);
	
	currentWindow.add(menuTopView);
	currentWindow.add(menuBottomView);
	
//	_createCommentWindow();
}

function _createCommentWindow()
{
	commentWindow = Titanium.UI.createWindow({
		url: 'commentWindow.js',
		orientationModes: [Titanium.UI.PORTRAIT_UP, Titanium.UI.UPSIDE_PORTRAIT],
		data: [
		       [{name: 'naam', comment: 'commentaar', date: 'datum'}, {name: 'naam', comment: 'commentaar', date: 'datum'}, {name: 'naam', comment: 'commentaar', date: 'datum'}],
		       [{name: 'naam', comment: 'commentaar', date: 'datum'}, {name: 'naam', comment: 'commentaar', date: 'datum'}, {name: 'naam', comment: 'commentaar', date: 'datum'}],
		       [{name: 'naam', comment: 'commentaar', date: 'datum'}, {name: 'naam', comment: 'commentaar', date: 'datum'}, {name: 'naam', comment: 'commentaar', date: 'datum'}],
		       [{name: 'naam', comment: 'commentaar', date: 'datum'}, {name: 'naam', comment: 'commentaar', date: 'datum'}, {name: 'naam', comment: 'commentaar', date: 'datum'}],
		       [{name: 'naam', comment: 'commentaar', date: 'datum'}, {name: 'naam', comment: 'commentaar', date: 'datum'}, {name: 'naam', comment: 'commentaar', date: 'datum'}],
		       [{name: 'naam', comment: 'commentaar', date: 'datum'}, {name: 'naam', comment: 'commentaar', date: 'datum'}, {name: 'naam', comment: 'commentaar', date: 'datum'}],
		       [{name: 'naam', comment: 'commentaar', date: 'datum'}, {name: 'naam', comment: 'commentaar', date: 'datum'}, {name: 'naam', comment: 'commentaar', date: 'datum'}],
		       [{name: 'naam', comment: 'commentaar', date: 'datum'}, {name: 'naam', comment: 'commentaar', date: 'datum'}, {name: 'naam', comment: 'commentaar', date: 'datum'}],
		       [{name: 'naam', comment: 'commentaar', date: 'datum'}, {name: 'naam', comment: 'commentaar', date: 'datum'}, {name: 'naam', comment: 'commentaar', date: 'datum'}],
		       [{name: 'naam', comment: 'commentaar', date: 'datum'}, {name: 'naam', comment: 'commentaar', date: 'datum'}, {name: 'naam', comment: 'commentaar', date: 'datum'}]
		        ]
	});
}

_windowSettings();
_createView();